package zhu.common.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.io.Serializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//最底层DAO接口的实现，实现对数据库进行增删查改的操作*/
public class BaseDAOImpl extends HibernateDaoSupport implements IBaseDAO {

	private static final Logger log = LoggerFactory.getLogger(BaseDAOImpl.class);
	
	public void save(Object object) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().save(object);
			log.debug("save " + object + " success !");
		} catch (RuntimeException e) {
			log.debug("save " + object + " failed !");
			e.printStackTrace();
			throw e;
		}
	}

	public void delete(Object object) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().delete(object);
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw e;
		}
	}

	public void update(Object object) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().update(object);
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw e;
		}
	}

	public List find(String where) {
		// TODO Auto-generated method stub
		try {
			return getHibernateTemplate().find(where);
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw e;
		}		
	}

	public void saveOrUpdate(Object object) {
		// TODO Auto-generated method stub
		//Boolean result=false;
		try {
			getHibernateTemplate().saveOrUpdate(object);
			//result= true;
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw e;
			
		}
		//return result;
		
	}

	public Object findById(Class cla, Object id) {
		// TODO Auto-generated method stub
		try {
			return getHibernateTemplate().get(cla, (Serializable) id);
		} catch (RuntimeException e) {
			System.out.println("find " + id + "failed");
			e.printStackTrace();
			throw e;
		}
	}

	public List findByProperty(String propertyName, Object value,String instanceType) {
		// TODO Auto-generated method stub
		log.debug("finding  instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from  "+ instanceType+ "   as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
		//getHibernateTemplate().execute(action)
	}

	public Object findByPropertyUnique(final String propertyName, final Object value,
			final String instanceType) {
		// TODO Auto-generated method stub
		return  (Object)getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "from "+ instanceType+"  where  " + propertyName + " = '"
								+ value + "'";
						Query query = session.createQuery(hql);
						query.setMaxResults(1);
						return query.uniqueResult();
						
					}
					//releaseSession(session);
				});
		
	}

	public Object merge(Object object) {
		
			log.debug("merging Client instance");
			try {
				Object result = (Object) getHibernateTemplate().merge(
						object );
				log.debug("merge successful");
				return result;
			} catch (RuntimeException re) {
				log.error("merge failed", re);
				throw re;
			}
		}
		

}
